package com.cn.wanxi.util.util;

import com.cn.wanxi.util.dao.CompanyDao;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class IOTool {


    static File file = null;

    private static void createNote(String fileName) {

        File fileDirectory = new File(MyPath.PATH.getPath());
        if (!fileDirectory.exists()) {
            fileDirectory.mkdirs();
        }
        file = new File(fileDirectory, fileName);
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    /**
     * 写入数据
     *
     * @param fileName 写入的文件名称
     * @param list     需要写入的数据，必须一集合的形式传入
     */
    public static void writeNote(String fileName, List<String> list) {
        createNote(fileName);
        try {
            OutputStream outputStream = new FileOutputStream(file);
            for (int i = 0; i < list.size(); i++) {
                outputStream.write(list.get(i).getBytes());
                outputStream.write(MyPath.SPLIT.getPath().getBytes());
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String readNote() {
        return read(file);
    }

    public static String readNote(String path) {
        File file = new File(path);
        return read(file);
    }

    private static String read(File file) {
        StringBuilder aa = new StringBuilder();
        try {
//            InputStream inputStream = new FileInputStream(file);
//            int result = 0;
//            while ((result = inputStream.read()) != -1) {
//                System.out.print((char) result);
//            }
            Reader reader = new FileReader(file);
            int result = 0;
//            String aa=""; aa+=(char) result;
//            String aa="aa";
//            String aa=new String("aa");
//            String和StringBuilder和StringBuffer的区别？
            while ((result = reader.read()) != -1) {
                aa.append((char) result);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return aa.toString();
    }

}
